package com.voiceblog.action;

import java.util.List;

import org.springframework.beans.support.PagedListHolder;

import com.voiceblog.model.Album;
import com.voiceblog.model.Blog;
import com.voiceblog.model.Users;

public class Manager extends BaseAction {
	private List<Users> userList;
	private Users user;
	private String account;
	private List<Blog> blogList;
	private List<Blog> albumList;
	private Album album;
	private int albumId;
	private String page;
	private PagedListHolder pageBlogList;
	private PagedListHolder pageAlbumListList;
	private PagedListHolder pageUserList;
	private int pageNum;

	public String addManager() throws Exception {
		this.user = this.getDao().findById(account);
		this.user.setUserType(Users.MANAGER);
		this.getDao().updateUser(user);
		return SUCCESS;

	}

	public String listUsers() throws Exception {
		this.userList = this.getDao().findAllUsers();
		if (page == null) {
			pageUserList = new PagedListHolder(userList);
			getSession().put("pageUserList", pageUserList);
		}
		pageUserList = (PagedListHolder) getSession().get("pageUserList");
		pageUserList.setPageSize(10);
		try {
			this.pager(page, pageUserList, pageNum - 1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public String deleteUser() throws Exception {
		this.user = this.getDao().findById(account);
		this.getDao().deleteUser(user);

		return SUCCESS;

	}

	public String listAllBlog() throws Exception {
		this.blogList = this.getDao().findTopBlogs(100000);
		if (page == null) {
			pageBlogList = new PagedListHolder(blogList);
			getSession().put("pageBlogList", pageBlogList);
		}
		pageBlogList = (PagedListHolder) getSession().get("pageBlogList");
		pageBlogList.setPageSize(10);
		try {
			this.pager(page, pageBlogList, pageNum - 1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public String listAllAlbum() throws Exception {
		this.albumList = this.getDao().findAllAlbum();
		if (page == null) {
			pageAlbumListList = new PagedListHolder(albumList);
			getSession().put("pageAlbumListList", pageAlbumListList);
		}
		pageAlbumListList = (PagedListHolder) getSession().get("pageAlbumListList");
		pageAlbumListList.setPageSize(10);
		try {
			this.pager(page, pageAlbumListList, pageNum - 1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public String deleteAlbum() throws Exception {
		this.album = this.getDao().findAlbumById(albumId);
		this.getDao().deleteAlbum(album);
		return SUCCESS;

	}

	public PagedListHolder getPageBlogList() {
		return pageBlogList;
	}

	public void setPageBlogList(PagedListHolder pageBlogList) {
		this.pageBlogList = pageBlogList;
	}

	public PagedListHolder getPageAlbumListList() {
		return pageAlbumListList;
	}

	public void setPageAlbumListList(PagedListHolder pageAlbumListList) {
		this.pageAlbumListList = pageAlbumListList;
	}

	public PagedListHolder getPageUserList() {
		return pageUserList;
	}

	public void setPageUserList(PagedListHolder pageUserList) {
		this.pageUserList = pageUserList;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public Album getAlbum() {
		return album;
	}

	public void setAlbum(Album album) {
		this.album = album;
	}

	public int getAlbumId() {
		return albumId;
	}

	public void setAlbumId(int albumId) {
		this.albumId = albumId;
	}

	public List<Blog> getAlbumList() {
		return albumList;
	}

	public void setAlbumList(List<Blog> albumList) {
		this.albumList = albumList;
	}

	public List<Blog> getBlogList() {
		return blogList;
	}

	public void setBlogList(List<Blog> blogList) {
		this.blogList = blogList;
	}

	public List<Users> getUserList() {
		return userList;
	}

	public void setUserList(List<Users> userList) {
		this.userList = userList;
	}

	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

}
